package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseHolder;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.HousePeopleBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/13 10:25
 */

public class HousePeopleAdapter extends RecyclerView.Adapter<HousePeopleAdapter.ViewHolder> {

    private Context context;
    private List<HousePeopleBean> data = new ArrayList<>();
    private List<String> selectedList = new ArrayList<>();

    public HousePeopleAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_house_people, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.instanceView(position);
    }

    @Override
    public int getItemCount() {
        return data == null?0:data.size();
    }

    public class ViewHolder extends BaseHolder{

        private TextView tv_name;
        private TextView tv_date;
        private boolean selected;
        private AppCompatCheckBox checkBox;

        public ViewHolder(View itemView) {
            super(itemView);
            selected = false;
        }

        @Override
        public void assignViews() {
            tv_name = (TextView) findViewById(R.id.tv_name);
            tv_date = (TextView) findViewById(R.id.tv_date);
            checkBox = (AppCompatCheckBox) findViewById(R.id.cb_check);
        }

        @Override
        public void instanceView(int position) {
            final HousePeopleBean housePeopleBean = data.get(position);
            tv_name.setText(housePeopleBean.getUser_name());
            tv_date.setText(housePeopleBean.getHouse_end_time());
            getItemView().setSelected(selected);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    selected = isChecked;
                    getItemView().setSelected(selected);
                    if (selected){
                        selectedList.add(housePeopleBean.getUser_mobile());
                    }else {
                        selectedList.remove(housePeopleBean.getUser_mobile());
                    }
                }
            });
            getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selected = !selected;
                    checkBox.setChecked(selected);
                }
            });
        }
    }

    public void notifyData(List<HousePeopleBean> data){
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public List<String> getSelectedPeople(){
        return selectedList;
    }

}
